package com.webamc.controller;

import com.webamc.common.enums.Fields;
import com.webamc.common.enums.Params;
import com.webamc.manager.ICustomerHandler;
import com.webamc.model.Customer;
import com.webamc.model.Result;
import com.webamc.model.User;
import com.webamc.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Date: Mar 14, 2009
 * Time: 12:09:55 AM
 */
public class CustomerAction extends AbstractAction {
    private Logger log = Logger.getLogger(CustomerAction.class);
    private ICustomerHandler customerHandler;

    public void setCustomerHandler(ICustomerHandler customerHandler) {
        this.customerHandler = customerHandler;
    }

    public ModelAndView update(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getCreateView());

        User u = getSessionUser(req);
        String action = req.getParameter(Params.ACTION);

        if (!isAuthorized(u, action)) {
            String msg = "Access Denied - role=" + u.getCategory()
                    + ", action=" + action + ", username=" + u.getUsername();
            log.info(msg);
            return setMV(mv, msg, getErrorView());
        }

        Map m = getParams(req);
        Result r = getValidator().validate(m);
        if (r != null && !r.isPassed()) {
            log.info("Parameter validation error" + r.getMsgs());
            return setMV(mv, r.getMsgs(), getErrorView());
        }
        try {
            Customer customer = customerHandler.update(m, u.getUserId());
            String msg = action + " action successful for Customer - " + customer.getCustomerId();
            mv.addObject(Params.BEAN, customer);
            setMV(mv, msg, getSingleView());
        } catch (Exception e) {
            log.error("Error occured while - " + e.getMessage(), e);
            setMV(mv, e.getMessage(), getErrorView());
        }

        log.info("Returning view =" + mv.getViewName());
        return mv;

    }

    public ModelAndView create(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getCreateView());

        User u = getSessionUser(req);
        String action = req.getParameter(Params.ACTION);

        if (!isAuthorized(u, action)) {
            String msg = "Access Denied - role=" + u.getCategory()
                    + ", action=" + action + ", username=" + u.getUsername();
            log.info(msg);
            return setMV(mv, msg, getErrorView());
        }

        Map m = getParams(req);
        Result r = getValidator().validate(m);
        if (r != null && !r.isPassed()) {
            log.info("Parameter validation error" + r.getMsgs());
            return setMV(mv, r.getMsgs(), getErrorView());
        }

        try {
            Customer customer = customerHandler.create(m, u.getUserId());
            String msg = action + " action successful for Customer - " + customer.getCustomerId();
            mv.addObject(Params.BEAN, customer);
            setMV(mv, msg, getSingleView());
        } catch (Exception e) {
            log.error("Error occured while - " + e.getMessage(), e);
            setMV(mv, e.getMessage(), getErrorView());
        }

        log.info("Returning view =" + mv.getViewName());
        return mv;
    }

    public ModelAndView view(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getCreateView());

        User u = getSessionUser(req);
        String action = req.getParameter(Params.ACTION);

        if (!isAuthorized(u, action)) {
            String msg = "Access Denied - role=" + u.getCategory()
                    + ", action=" + action + ", username=" + u.getUsername();
            log.info(msg);
            return setMV(mv, msg, getErrorView());
        }

        try {
            Map m = getParams(req);
            int customerId = CommonUtil.getInt(m, Fields.CUSTOMERID, 0);
            Customer customer = customerHandler.view(customerId);
            mv.addObject(Params.BEAN, customer);
            mv.setViewName(getSingleView());
        } catch (Exception e) {
            log.error("Error occured while - " + e.getMessage(), e);
            setMV(mv, e.getMessage(), getErrorView());
        }

        log.info("Returning view =" + mv.getViewName());
        return mv;

    }

    public ModelAndView beforeUpdate(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = view(req, res);
        mv.setViewName(getBeforeUpdateView());
        return mv;
    }

    public ModelAndView beforeCreate(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getBeforeCreateView());
        return mv;
    }

    public ModelAndView beforeSearch(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getBeforeSearchView());
        return mv;
    }

    public ModelAndView search(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getListView());

        User u = getSessionUser(req);
        String action = req.getParameter(Params.ACTION);

        if (!isAuthorized(u, action)) {
            String msg = "Access Denied - role=" + u.getCategory()
                    + ", action=" + action + ", username=" + u.getUsername();
            log.info(msg);
            return setMV(mv, msg, getErrorView());
        }

        try {
            Map m = getParams(req);
            List customers = customerHandler.search(m);
            mv.addObject(Params.BEAN, customers);
            log.info("List of customers returned with size=" + customers.size());
        } catch (Exception e) {
            log.error("Error occured while - " + e.getMessage(), e);
            setMV(mv, e.getMessage(), getErrorView());
        }

        log.info("Returning view =" + mv.getViewName());
        return mv;
    }

    public ModelAndView browse(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mv = new ModelAndView(getListView());

        User u = getSessionUser(req);
        String action = req.getParameter(Params.ACTION);

        if (!isAuthorized(u, action)) {
            String msg = "Access Denied - role=" + u.getCategory()
                    + ", action=" + action + ", username=" + u.getUsername();
            log.info(msg);
            return setMV(mv, msg, getErrorView());
        }

        try {
            List customers = customerHandler.browse();
            log.info("List of customers returned with size=" + customers.size());
            mv.addObject(Params.BEAN, customers);
        } catch (Exception e) {
            log.error("Error occured while creating - " + e.getMessage(), e);
            setMV(mv, e.getMessage(), getErrorView());
        }

        log.info("Returning view =" + mv.getViewName());
        return mv;
    }

    protected ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException noSuchRequestHandlingMethodException, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        return super.handleNoSuchRequestHandlingMethod(noSuchRequestHandlingMethodException, httpServletRequest, httpServletResponse);    //To change body of overridden methods use File | Settings | File Templates.
    }
}
